# encoding:utf-8

import picross_functions as pfun
import numpy
import sys
import datetime as dt

start_time = dt.datetime.now()

# s_in_size : 行数 x 列数
# s_in_row ： 每行的绘图值
# s_in_column : 每列的绘图值
map_file = open('map.txt', mode='r', encoding='utf-8')
a = map_file.readline()
while a and a != 'r\n':
    a = map_file.readline()

a = map_file.readline()
s_in_row = []
while a and a != 'c\n':
    if a == '\n':
        s_in_row.append([])
        a = map_file.readline()
        continue
    t = eval(a)
    if type(t) == int:
        s_in_row.append([t])
    else:
        s_in_row.append(list(t))
    a = map_file.readline()

a = map_file.readline()
s_in_column = []
while a:
    if a == '\n':
        s_in_column.append([])
        a = map_file.readline()
        continue
    t = eval(a)
    if type(t) == int:
        s_in_column.append([t])
    else:
        s_in_column.append(list(t))
    a = map_file.readline()

map_file.close()

s_in_size = [len(s_in_row), len(s_in_column)]

# 初始化结果图
temp_map = [-1 for i in range(s_in_size[1])]
resu_map = [temp_map.copy() for i in range(s_in_size[0])]

# todo : 容错
# 检查行列数是否有问题，检查输入参数是否小于等于行列数

# 求解
# 循环一次所有行，并循环一次所有列
# 循环过程中
#   如果只有一个结果，则记录下结果，并标记/剔除该行或列
#   如果所有的解有共同的结果，也记录下结果。
# 重复上面的循环
# 循环正常退出条件：找到所有行和列
# 避免死循环：经过一轮循环之后，没有剔除任何行列

row_loop = [i for i in range(s_in_size[0])]
column_loop = [i for i in range(s_in_size[1])]
p_twice = 0
while len(row_loop) + len(column_loop) > 0:
    p_change = False
    for i in row_loop:
        p_solutions = pfun.line_solutions(s_in_size[0], resu_map[i], s_in_row[i])
        if len(p_solutions) == 1:
            for j in range(s_in_size[1]):
                resu_map[i][j] = p_solutions[0][j]
            row_loop.remove(i)
            p_change = True
            continue
        for j in range(s_in_size[1]):
            value_j = p_solutions[0][j]
            p_same = True
            for k in p_solutions[1:]:
                if k[j] != value_j:
                    p_same = False
                    break
            if p_same:
                resu_map[i][j] = value_j
    for i in column_loop:
        p_line = list(numpy.array(resu_map)[:, i])
        p_solutions = pfun.line_solutions(s_in_size[1], p_line, s_in_column[i])
        if len(p_solutions) == 1:
            for j in range(s_in_size[0]):
                resu_map[j][i] = p_solutions[0][j]
            column_loop.remove(i)
            p_change = True
            continue
        for j in range(s_in_size[0]):
            value_j = p_solutions[0][j]
            p_same = True
            for k in p_solutions[1:]:
                if k[j] != value_j:
                    p_same = False
                    break
            if p_same:
                resu_map[j][i] = value_j
    if not p_change:
        print('无解')
        for i in resu_map:
            print(i)
        sys.exit(1)

# 打印结果
for i in resu_map:
    print(i)

for i in resu_map:
    prline = ''
    for j in i:
        if j == 1:
            prline += '**'
        else:
            prline += '  '
    print(prline)
end_time = dt.datetime.now()

print('耗时：', end_time - start_time)
